const express = require('express');
const storyCtrl = require('../controller/story');
const storyTypeCtrl = require('../controller/storytype');
const captureCtrl = require('../controller/capture');
const utils = require('../utils/utils');
const auth = require('../middleware/auth');
const router = express.Router();

//显示小说列表页面
router.get('/', auth.loginRequired, (req, res, next) => {
	global.navlist = 1;
	storyCtrl.getStoryList(req.query.page, data => {
		res.render('list_story', { count: data.count, list: data.rows });
	})
});

//获取上传图片列表
router.get('/getImg', (req, res, next) => {
	storyCtrl.getImageList(data => {
		res.render('list_image', { list: data });
	})
});

//添加小说页面
router.get('/add', (req, res, next) => {
	storyTypeCtrl.getBookTypeList(data => {
		captureCtrl.getCaptureList(result => {
			res.render('add_story', { types: data, sites: result });
		})
	})
});

//编辑小说页面
router.get('/edit', (req, res, next) => {
	storyTypeCtrl.getBookTypeList(result => {
		captureCtrl.getCaptureList(sites => {
			storyCtrl.getStoryOne(req.query.id, data => {
				let datas = JSON.parse(data);
				if (!!datas && typeof datas == 'object') {
					res.render('edit_story', { datas: data, types: result, sites });
				} else {
					res.status(404);
					next();
				}
			})
		})
	})
});

//显示章节列表页面
router.get('/:bookno', (req, res, next) => {
	let bookno = req.params.bookno;
	if (utils.isBookNo(bookno)) {
		storyCtrl.getStoryInfo(bookno, data => {
			res.render('list_chapter', data);
		})
	} else {
		res.status(404);
		next();
	}
});

//添加章节页面
router.get('/:bookno/add', (req, res, next) => {
	console.log(req.params.bookno);
	/*storyCtrl.getStoryInfo(req.params.bookno, data => {
		res.render('add_chapter', {
			data: JSON.parse(data)
		});
	})*/
});

//修改章节页面
router.get('/:bookno/edit', (req, res, next) => {
	storyCtrl.getStoryInfo(req.params.bookno, data => {
		res.render('edit_chapter', {
			data: JSON.parse(data)
		});
	})
});

//同步章节操作
router.get('/:bookno/sync', (req, res, next) => {
	res.render('sync_chapter');
});

//添加小说请求
router.post('/create', (req, res, next) => {
	storyCtrl.createStory(req.body, data => {
		if (data) {
			res.json({
				code: 200,
				success: true,
				msg: '添加小说成功'
			});
		} else {
			res.json({
				code: 200,
				success: false,
				msg: '添加小说失败'
			});
		}
	})
});

//修改小说操作
router.post('/update', (req, res, next) => {
	storyCtrl.updateStory(req.body.id, req.body, data => {
		if (data) {
			res.json({
				code: 200,
				success: true,
				msg: '更新小说成功'
			});
		} else {
			res.json({
				code: 200,
				success: false,
				msg: '更新小说失败'
			});
		}
	})
});

//删除小说
router.post('/delete', (req, res, next) => {
	storyCtrl.delStory(req.body.id, data => {
		if (data) {
			res.json({
				code: 200,
				success: true,
				msg: '删除小说成功'
			});
		} else {
			res.json({
				code: 200,
				success: false,
				msg: '删除小说失败'
			});
		}
	})
});

//添加章节操作
//router.post('/:bookno/create', storyCtrl.createChapter);

//修改章节操作
//router.post('/:bookno/update', storyCtrl.updateChapter);

module.exports = router;